/*
************************************************************************************
  * @author  ZLP
  * @version 1.0
  * @date    2024.1.16
************************************************************************************
*/
#include "Math.h"

/**
 * @brief 快速排序
 * @param origin
 * @param low
 * @param high
*/
int quick_sort(uint32_t *a, uint32_t low, uint32_t high)
{
	uint32_t i = low;	
	uint32_t j = high;	
	uint32_t key = a[i]; 
 
	while (i < j)
	{

		while(i < j && a[j] >= key)
		{
			j--;
		}
		a[i] = a[j];	

		while(i < j && a[i] <= key)
		{
			i++;
		}
		a[j] = a[i];
	}
 
	a[i] = key;
	if (i-1 > low) 
	{
		quick_sort(a, low, i-1);
	}
 
	if (i+1 < high)
	{
		quick_sort(a, i+1, high);
	}
 
	return 0;
} 

/**
 * @brief 二分查找
 * @param origin 
 * @param targe 
 * @param lengh 
 * @return middle
*/

int Binary_search(uint32_t* origin ,uint32_t targe,uint16_t lengh)
{
    uint16_t left = 0;
    uint16_t right = lengh - 1;
    uint16_t middle = 0;
    while(left<=right)
    {
        middle = ( left + right)/2;
        if(targe > origin[middle])
        {
            left = middle + 1;
        }else if( targe < origin[middle] )
        {
            right = middle -1;
        }else
        {
            return middle;
        }

    }
    return 0;
}
